package cn.anecansaitin.firecrafting.common.crafting;

import cn.anecansaitin.firecrafting.api.common.crafting.IFireRecipe;
import com.google.common.collect.ImmutableList;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.util.RecipeMatcher;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public abstract class SimpleFireRecipe implements IFireRecipe {
    protected final NonNullList<Ingredient> inputs;
    protected final ImmutableList<Block> fireBlock;
    protected final ResourceLocation id;

    public SimpleFireRecipe(ResourceLocation id, ImmutableList<Block> fireBlock, NonNullList<Ingredient> inputs) {
        this.id = id;
        this.inputs = inputs;
        this.fireBlock = fireBlock;
    }

    //判断配方是否符合
    @Override
    public boolean match(List<ItemStack> stacks, BlockPos pos, Level world) {
        //判断火焰是否符合
        if (matchFire(pos, world)) {
            //检测输入物品与配方需求是否符合
            int[] matches = RecipeMatcher.findMatches(stacks, inputs);
            //不为null则表示检测成功
            return matches != null;
        }
        return false;
    }

    @Override
    public boolean match(List<ItemStack> stacks, Block fire) {
        //判断火焰是否符合
        if (matchFire(fire)) {
            //检测输入物品与配方需求是否符合
            int[] matches = RecipeMatcher.findMatches(stacks, inputs);
            //不为null则表示检测成功
            return matches != null;
        }
        return false;
    }

    //检测指定位置火焰
    @Override
    public boolean matchFire(BlockPos pos, Level world) {
        //获取指定位置的方块信息
        BlockState state = world.getBlockState(pos);
        //返回结果
        return matchFire(state.getBlock());
    }

    //检测传入火焰
    @Override
    public boolean matchFire(Block fire) {
        return fireBlock.contains(fire);
    }

    //返回配方指定的物品成分
    @Override
    public @NotNull NonNullList<Ingredient> getIngredients() {
        return inputs;
    }

    @Override
    public @NotNull ResourceLocation getId() {
        return id;
    }

    @Override
    public List<Block> getFires() {
        return fireBlock;
    }
}
